import { request } from '@/request'

const state = {
    versionList: [],
    modelList: [],
    loading: false,
    total: 0
}

const mutations = {
    SET_VERSION_LIST(state, list) {
        state.versionList = list
    },
    SET_MODEL_LIST(state, list) {
        state.modelList = list
    },
    SET_LOADING(state, loading) {
        state.loading = loading
    },
    SET_TOTAL(state, total) {
        state.total = total
    }
}

const actions = {
    // 获取版本列表
    async getVersionList({ commit }, params = {}) {
        commit('SET_LOADING', true)
        try {
            const res = await request({
                url: '/api/versions',
                method: 'get',
                params: {
                    page: params.page || 1,
                    pageSize: params.pageSize || 10,
                    search: params.search || ''
                }
            })
            if (res.code === 200) {
                commit('SET_VERSION_LIST', res.data.list || [])
                commit('SET_TOTAL', res.data.total || 0)
            }
            return res
        } catch (error) {
            console.error('获取版本列表失败:', error)
            throw error
        } finally {
            commit('SET_LOADING', false)
        }
    },

    // 获取模型列表
    async getModelList({ commit }, params = {}) {
        commit('SET_LOADING', true)
        try {
            const res = await request({
                url: '/api/models',
                method: 'get',
                params: {
                    page: params.page || 1,
                    pageSize: params.pageSize || 20,
                    search: params.search || ''
                }
            })
            if (res.code === 200) {
                commit('SET_MODEL_LIST', res.data.list || [])
                commit('SET_TOTAL', res.data.total || 0)
            }
            return res
        } catch (error) {
            console.error('获取模型列表失败:', error)
            throw error
        } finally {
            commit('SET_LOADING', false)
        }
    },

    // 上传版本
    async uploadVersion({ dispatch }, data) {
        try {
            const res = await request({
                url: '/api/versions/upload',
                method: 'post',
                headers: {
                    'Content-Type': 'multipart/form-data'
                },
                data
            })
            if (res.code === 200) {
                // 重新获取列表
                await dispatch('getVersionList')
            }
            return res
        } catch (error) {
            console.error('上传版本失败:', error)
            throw error
        }
    },

    // 上传模型
    async uploadModel({ dispatch }, data) {
        try {
            const res = await request({
                url: '/api/models/upload',
                method: 'post',
                headers: {
                    'Content-Type': 'multipart/form-data'
                },
                data
            })
            if (res.code === 200) {
                // 重新获取列表
                await dispatch('getModelList')
            }
            return res
        } catch (error) {
            console.error('上传模型失败:', error)
            throw error
        }
    },

    // 更新版本
    async updateVersion({ dispatch }, { id, data }) {
        try {
            const res = await request({
                url: `/api/versions/${id}`,
                method: 'put',
                data
            })
            if (res.code === 200) {
                await dispatch('getVersionList')
            }
            return res
        } catch (error) {
            console.error('更新版本失败:', error)
            throw error
        }
    },

    // 更新模型
    async updateModel({ dispatch }, { id, data }) {
        try {
            const res = await request({
                url: `/api/models/${id}`,
                method: 'put',
                headers: {
                    'Content-Type': 'multipart/form-data'
                },
                data
            })
            if (res.code === 200) {
                await dispatch('getModelList')
            }
            return res
        } catch (error) {
            console.error('更新模型失败:', error)
            throw error
        }
    },

    // 删除版本
    async deleteVersion({ dispatch }, id) {
        try {
            const res = await request({
                url: `/api/versions/${id}`,
                method: 'delete'
            })
            if (res.code === 200) {
                await dispatch('getVersionList')
            }
            return res
        } catch (error) {
            console.error('删除版本失败:', error)
            throw error
        }
    },

    // 删除模型
    async deleteModel({ dispatch }, id) {
        try {
            const res = await request({
                url: `/api/models/${id}`,
                method: 'delete'
            })
            if (res.code === 200) {
                await dispatch('getModelList')
            }
            return res
        } catch (error) {
            console.error('删除模型失败:', error)
            throw error
        }
    },

    // 批量删除版本
    async batchDeleteVersions({ dispatch }, ids) {
        try {
            const res = await request({
                url: '/api/versions/batch',
                method: 'delete',
                data: { ids }
            })
            if (res.code === 200) {
                await dispatch('getVersionList')
            }
            return res
        } catch (error) {
            console.error('批量删除版本失败:', error)
            throw error
        }
    },

    // 批量删除模型
    async batchDeleteModels({ dispatch }, ids) {
        try {
            const res = await request({
                url: '/api/models/batch',
                method: 'delete',
                data: { ids }
            })
            if (res.code === 200) {
                await dispatch('getModelList')
            }
            return res
        } catch (error) {
            console.error('批量删除模型失败:', error)
            throw error
        }
    }
}

const getters = {
    versionList: state => state.versionList,
    modelList: state => state.modelList,
    loading: state => state.loading,
    total: state => state.total
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}